<template>
    <el-drawer v-model="childConfig.DrawerStatus" :title="childConfig.title" :before-close="handleClose" size="50%">
        <el-form ref="ruleForm" label-width="100px" :model="formData" label-position="top" :rules="rules">
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="部门助理">
                        <el-select v-model="formData.userIds" placeholder="请选择助理" class="file-name" clearable multiple filterable>
                            <el-option v-for="item in userData.userSelectList" :key="item.userId" :label="item.username" :value="item.userId"></el-option>
                        </el-select>
                </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <div class="drawer-footer">
            <el-button @click="handleClose(false)">取消</el-button>
            <el-button type="primary" @click="confirmButtonHandle">确定</el-button>
        </div>
    </el-drawer>
    
</template>


<script setup>
import {reactive, toRefs, ref, computed} from 'vue'
import { useStore } from 'vuex'
import {setAssistant} from '^/organization/department.js'

// 获取用户数据
const store = useStore()
const userData = computed(() => store.getters.user)
userData.value.userSelectList.length || store.dispatch('getUserDownList')

// 接收父组件值
const props = defineProps({
    childConfig: {
        type: Object
    },
    formData: {
        type: Object
    }
})
// 在定义emits的事件，才能通过emits给指定的事件修改父组件的值
const emits = defineEmits(['upDrawerStatus', 'upViewList'])

// 接收父组件值
const {childConfig, formData} = toRefs(props)

const ruleForm = ref(null)


// 表单校验
const rules = reactive({
    roleName: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '说明不能为空', trigger: 'blur' }]
})


/**
 * 接口api
 */

// 新增角色
const methods = {
    editHandleApi: () => {
        ruleForm.value.validate((vaild) => {
            if (vaild){
                setAssistant(formData.value).then(res => {
                    console.log("设置部门助理成功：", res.data.data, res.data.message)
                    handleClose()
                }, err => {
                    console.log("设置部门助理异常", err)
                })
            } else {
                console.log("验证失败", vaild)
            }
        })
    }
}


/**
 * 界面操作api
 */
/**
 * 
 */
const handleClose = (isUpdate = true) => {
    console.log('关闭抽屉')
    emits('upDrawerStatus', false)
    isUpdate && emits('upViewList')
}
const confirmButtonHandle = () => {
    methods[childConfig.value.confirmFunctionName]()
}
</script>


<style lang="scss" scoped>
.drawer-footer{
    position: absolute;
    bottom: 15px;
    right: 15px;
}

.file-name{
    width: 100%;
}
</style>